﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Data;
using System.ServiceModel;

namespace ConfExpert.Forms.SatinAlma
{
    public partial class UNSPED_Tables : XForm
    {
        public UNSPED_Tables()
        {
            InitializeComponent();
        }

        private void UNSPED_Tables_Load(object sender, EventArgs e)
        {
            int Donem = cmbDonem.EditValue.ToInt();
            Notify(Unsped_Util.Rfrsh(Donem));


            gridView1.Init("GetUNSPED_Tablolar", Donem);
            gridView1.AddDetailGrid(gridView2, "Document_ROW_Id", 1);
            gridView1.AddDetailGrid(gridView3, "Document_ROW_Id", 2);
            gridView1.AddDetailGrid(gridView4, "Document_ROW_Id", 3);
            gridView1.AddDetailGrid(gridView5, "Document_ROW_Id", 4);
            gridView1.AddDetailGrid(gridView6, "Document_ROW_Id", 5);

            gridView1.AddColumn("DEK", typeof(int), "COUNT(Child({FK}).Document_ROW_Id)", 0);   //TODO: bunlar yukardakinin aksine niye 1 geriden geliyor, araştırılacak!
            gridView1.AddColumn("FAT", typeof(int), "COUNT(Child({FK}).Document_ROW_Id)", 1);
            gridView1.AddColumn("FTB", typeof(int), "COUNT(Child({FK}).Document_ROW_Id)", 2);
            gridView1.AddColumn("IST", typeof(int), "COUNT(Child({FK}).Document_ROW_Id)", 3);
            gridView1.AddColumn("VER", typeof(int), "COUNT(Child({FK}).Document_ROW_Id)", 4);

            gridView1.MakeColumnInvisible("Document_ROW_Id", "TIP", "FIRMA");
            gridView2.MakeColumnInvisible("DEKONTLAR_Id", "Document_ROW_Id");
            gridView3.MakeColumnInvisible("FATURALAR_Id", "Document_ROW_Id");
            gridView4.MakeColumnInvisible("FATURABILGILERI_Id", "Document_ROW_Id");
            gridView5.MakeColumnInvisible("ISTAKIPLER_Id", "Document_ROW_Id");
            gridView6.MakeColumnInvisible("VERGILER_Id", "Document_ROW_Id");

            gridView2.FormatAsNumber(2, "Tutar");
            gridView2.AddSummary("Tutar");

            gridView3.FormatAsNumber(2, "Tutar");
            gridView3.AddSummary("Tutar");

            gridView5.FormatAsDateTime("TarihSaat");

            gridView6.FormatAsNumber(2, "VergiTutari");
            gridView6.AddSummary("VergiTutari");

            gridView1.FormatAsDateTime("DOSYA_ACILIS_TARIHI", "TARIH_SAAT");
            gridView1.FormatAsDate("FATURA_TARIHI", "MAL_TESLIM_TARIHI", "OZET_BEYAN_TARIHI", "TESCIL_TARIH", "ISLEM_BITIS_TARIHI");
            gridView1.FormatAsNumber(2, "FATURA_TUTARI", "MIKTAR", "BRUTKG", "ISTATISTIKI");
            gridView1.FormatAsNumber(0, "DEK", "FAT", "IST", "VER", "FTB", "KAP", "FARK_GUN");
            gridView1.AddSummary("DEK", "FAT", "IST", "VER", "FTB", "KAP", "FATURA_TUTARI", "BRUTKG", "ISTATISTIKI");
            gridView1.YanYanaGetirSoluna("TUR", "DEK", "FAT", "FTB", "IST", "VER");
            gridView1.YanYanaGetir("REFERANS:UNSPED_DOSYA_NO");
            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;

            gridView1.MakeRowSelectable();
            gridView1_FocusedRowChanged(null, null);
            gridView1.BestFitColumns();

            //if (Eksik > 0)
            //    XtraMessageBox.Show(Eksik.ToStr() + " adet masraf kaleminin hesap kodu belli değil. Masrafların doğru hesaplanabilmesi için lütfen bunları eşleme formunda doldurun.");

            cmbDonem.EditValueChanged += cmbDonem_EditValueChanged;
        }

        void cmbDonem_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.RefreshDataSource("GetUNSPED_Tablolar", cmbDonem.EditValue.ToInt());
            gridView1_FocusedRowChanged(null, null);
            gridView1.BestFitColumns();
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            tpDEK.Text = "DEKONT." + gridView2.RowCount.ToStr();
            tpFAT.Text = "FATURA." + gridView3.RowCount.ToStr();
            tpFTB.Text = "FATURABILGI." + gridView4.RowCount.ToStr();
            tpIST.Text = "ISTAKIP." + gridView5.RowCount.ToStr();
            tpVER.Text = "VERGI." + gridView6.RowCount.ToStr();

            tpDEK.PageVisible = gridView2.RowCount > 0;
            tpFAT.PageVisible = gridView3.RowCount > 0;
            tpFTB.PageVisible = gridView4.RowCount > 0;
            tpIST.PageVisible = gridView5.RowCount > 0;
            tpVER.PageVisible = gridView6.RowCount > 0;

            if (e != null)
                if (e.FocusedRowHandle > -1)
                    txtInfo.Caption = gridView1.GetFocusedDataRow()["FATURA_NO"].ToStr();

            gridView2.BestFitColumns();
            gridView3.BestFitColumns();
            gridView4.BestFitColumns();
            gridView5.BestFitColumns();
            gridView6.BestFitColumns();
        }
    }
}
